package com.example.fw;

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.example.tests.ContactData;
import com.example.utils.SortedListOf;

public class ContactHelper extends HelperBase
{
	
	public ContactHelper(ApplicationManager manager) {
		super(manager);
	}
	
	private SortedListOf<ContactData> cashedContacts;
	
	public SortedListOf<ContactData> getContacts() {
		if (cashedContacts == null)
			cashedContacts = rebuildcashedContacts();
		return cashedContacts;	
	}
	
	private SortedListOf<ContactData> rebuildcashedContacts() {
		cashedContacts = new SortedListOf<ContactData>();
		manager.navigateTo().mainPage();
		List<WebElement> rows = findElements(By.xpath("//tr[@name='entry']"));
		for (WebElement row : rows) {
		  List<WebElement> cells = row.findElements(By.tagName("td"));
		  ContactData contact = new ContactData()
		  	.withFirstname(cells.get(2).getText())
		  	.withLastname(cells.get(1).getText());
		cashedContacts.add(contact);
		}
		return cashedContacts;
	}

	
	public ContactHelper createContact(ContactData grcontact) {
		openContactPage();
		fillContactCreationForm(grcontact);
		submitContactCreation();
		manager.navigateTo().mainPage();
		rebuildcashedContacts();
	    return this;
	}
	
	public ContactHelper modifyContact(int index, ContactData grcontact) {		
		initContactModification(index);
		fillContactModificationForm(grcontact);
		submitContactModification();
		manager.navigateTo().mainPage();
		rebuildcashedContacts();
	    return this;
	}	
	
	public ContactHelper deleteContact(int index) {
		manager.navigateTo().mainPage();
		selectContactByIndex(index);
		submitContactDeletion();
		manager.navigateTo().mainPage();
		rebuildcashedContacts();
	    return this;

	}

	
//-------------------------------------------------------------------------------------------------
	
	public void openContactPage() {
	    click(By.linkText("add new"));
	}
	

	public void initContactModification(int index) {
		selectContactByIndex(index);
	}
//	public ContactHelper openAllContacts() {
//		driver.get(manager.baseUrl + "/addressbookv4.1.4/addressbookv4.1.4/");
//	    return this;
//	    
//	}
	
	private void selectContactByIndex(int index) {
		click(By.xpath("(//img[@alt='Edit'])[" + (index+1) + "]"));
	}

	private void submitContactDeletion() {
		click(By.xpath("(//input[@name='update'])[2]"));
	}

	public void submitContactCreation() {
	    click(By.name("submit"));
	}
	
	public void submitContactModification() {
		click(By.xpath("(//input[@name='update'])[1]"));
	}
	
	public void fillContactCreationForm(ContactData grcontact) {
		fillCommonContactForm(grcontact);
		selectByText(By.name("new_group"), grcontact.getGroup());	    
	}

	public void fillContactModificationForm(ContactData grcontact) {
		 fillCommonContactForm(grcontact);
		 if (findElements(By.name("new_group")).size() != 0)
				throw new Error ("Group selector exists in contact modification form");		 
	}	

	private void fillCommonContactForm(ContactData grcontact) {
		type(By.name("firstname"),grcontact.getFname());
		 type(By.name("lastname"),grcontact.getLname());
		 type(By.name("address"),grcontact.getAddress1());
		 type(By.name("home"),grcontact.getHome());	 
		 type(By.name("mobile"),grcontact.getMobile());
		 type(By.name("work"),grcontact.getWork());
		 type(By.name("email"),grcontact.getEmail());
		 type(By.name("email2"),grcontact.getEmail2());
		 selectByText(By.name("bday"), grcontact.getBday());
		 selectByText(By.name("bmonth"), grcontact.getBmonth()); 
		 type(By.name("byear"),grcontact.getByear());
		 type(By.name("address2"),grcontact.getAddress2());
		 type(By.name("phone2"),grcontact.getPhone2());
	}
}
